/*
 * Copyright (C), 2002-2016, 苏宁易购电子商务有限公司
 * FileName: ModelBlendDaoServiceImpl.java
 * Author:   13075787
 * Date:     2016年7月4日 下午7:17:57
 * Description: //模块目的、功能描述      
 * History: //修改记录
 * <author>      <time>      <version>    <desc>
 * 修改人姓名             修改时间            版本号                  描述
 */
package com.suning.sawp.service.impl.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.suning.framework.dal.client.DalClient;
import com.suning.sawp.intf.model.ModelCheckVo;
import com.suning.sawp.intf.model.ModelConstant;
import com.suning.sawp.intf.model.ModelMainListBean;
import com.suning.sawp.intf.model.ModelProblemListBean;
import com.suning.sawp.intf.model.ModelTaskAssignDetailsBean;
import com.suning.sawp.service.util.BeanUtil;
import com.suning.sawp.service.util.DateUtils;

/**
 * 样机管理DAO<br>
 * 1盘点提交
 *
 * @author 13075787
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Service("modelBlendDaoService")
public class ModelBlendDaoServiceImpl {

    private static final int NUM_ZERO = 0;

    /**
     * 最大每页分页数
     */
    private static final int MAX_SIZE = 20;

    @Resource
    DalClient dalClient;

    @Autowired
    ModelTaskDaoServiceImpl modelTaskDaoService;

    @Resource
    ModelBlendDaoServiceImpl modelBlendDaoService;

    /**
     * 功能描述: <br>
     * 盘点提交
     *
     * @param taskId
     * @param checkStaffId
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @Transactional
    public boolean updateCheckStatusSumbit(Long taskId, String storeCode, String checkStaffId) {
        boolean result = true;
        if (null != taskId && StringUtils.isNotBlank(checkStaffId)) {
            List<ModelTaskAssignDetailsBean> upCheckList = new ArrayList<ModelTaskAssignDetailsBean>();
            ModelTaskAssignDetailsBean upCheck = new ModelTaskAssignDetailsBean();
            upCheck.setAcceptStaff(checkStaffId);
            upCheck.setTaskId(taskId);
            upCheck.setStoreCode(storeCode);
            upCheckList.add(upCheck);
            modelBlendDaoService.updateCheckStatusSumbit(upCheckList);
        }
        return result;
    }

    /**
     * 功能描述: <br>
     * 盘点提交更新状态
     *
     * @param upCheckList
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @SuppressWarnings("unchecked")
    @Transactional
    public int[] updateCheckStatusSumbit(List<ModelTaskAssignDetailsBean> upCheckList) {
        int[] result = null;
        if (CollectionUtils.isNotEmpty(upCheckList)) {
            Map<String, Object>[] batchValues1 = new HashMap[upCheckList.size()];
            Map<String, Object>[] batchValues2 = new HashMap[upCheckList.size()];
            for (int i = 0; i < upCheckList.size(); i++) {
                ModelTaskAssignDetailsBean upCheck = upCheckList.get(i);
                Map<String, Object> paramMap = new HashMap<String, Object>();
                // 更新条件
                paramMap.put("conditon_taskId", upCheck.getTaskId());
                paramMap.put("conditon_checkStaff", upCheck.getAcceptStaff());
                paramMap.put("conditon_storeCode", upCheck.getStoreCode());
                // 要更新为的状态
                paramMap.put("status", ModelConstant.CHECK_TASK_STATUS_DONE);
                // 更新任务详细分配表中的任务提交状态-修改为已提交
                Map<String, Object> paramMap1 = new HashMap<String, Object>();
                paramMap1.putAll(paramMap);
                // 未提交和提交的状态的任务都可以更新状态
                paramMap1.put("conditon_status1", ModelConstant.CHECK_TASK_STATUS_NO);
                batchValues1[i] = paramMap1;
                // 更新盘点任务列表中的提交状态
                Map<String, Object> paramMap2 = new HashMap<String, Object>();
                paramMap2.putAll(paramMap);
                // 更新必须满足-状态前提需要是盘点中的数据
                paramMap2.put("conditon_status", ModelConstant.CHECK_TASK_STATUS_NO);
                batchValues2[i] = paramMap2;
            }
            int[] ret1 = dalClient.batchUpdate("MODEL_CHECK_LIST.UPDATE_TASK_DETAIL_CHECK_STATUS", batchValues1);
            if (null != ret1) {
                result = dalClient.batchUpdate("MODEL_CHECK_LIST.UPDATE_MODEL_CHECK_STATUS", batchValues2);
            }
        }
        return result;
    }

    /**
     * 功能描述: <br>
     * 查询样机清单中某个商品的信息
     * 
     * @param storeCode
     * @param commCode
     * @return 返回空则该商品不存在
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public ModelMainListBean queryModelCommInfo(String storeCode, String commCode) {
        ModelMainListBean result = null;
        if (StringUtils.isNotBlank(storeCode) && StringUtils.isNotBlank(commCode)) {
            Map<String, String> paramMap = new HashMap<String, String>();
            paramMap.put("storeCode", storeCode);
            paramMap.put("commCode", commCode);
            result = dalClient.queryForObject("MODEL_MAIN_LIST.SELECT_MODEL_COMM_INFO", paramMap,
                    ModelMainListBean.class);
        }
        return result;
    }

    /**
     * 功能描述: <br>
     * 获取指定门店下指定商品编码的样机属性信息列表
     *
     * @param storeCode
     * @param commCodes
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public Map<String, Integer> queryModelAttributes(String storeCode, List<String> commCodes) {
        Map<String, Integer> result = new HashMap<String, Integer>();
        if (StringUtils.isNotBlank(storeCode) && CollectionUtils.isNotEmpty(commCodes)) {
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("storeCode", storeCode);
            paramMap.put("commCodes", commCodes);
            List<ModelCheckVo> list = dalClient.queryForList("MODEL_MAIN_LIST.SELECT_MODEL_COMM_ATTRIBUTES", paramMap,
                    ModelCheckVo.class);
            if (CollectionUtils.isNotEmpty(list)) {
                for (ModelCheckVo modelCheckVo : list) {
                    result.put(modelCheckVo.getCommCode(), modelCheckVo.getAttribute());
                }
            }
        }
        return result;
    }

    /**
     * 功能描述: <br>
     * 更新样机清单中样机的属性
     *
     * @param storeCode
     * @param commCode
     * @param attribute
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public boolean updateModelMainAttribute(String storeCode, String commCode, Integer attribute) {
        boolean result = false;
        if (StringUtils.isNotBlank(storeCode) && StringUtils.isNotBlank(commCode)) {
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("conditon_storeCode", storeCode);
            paramMap.put("conditon_commCode", commCode);
            paramMap.put("attribute", attribute);
            dalClient.execute("MODEL_MAIN_LIST.UPDATE_MODEL_MAIN_ATTRIBUTE", paramMap);
            result = true;
        }
        return result;
    }

    /**
     * 功能描述: <br>
     * 查询指定门店下，指定品类品牌的商品列表
     *
     * @param storeCode
     * @param brandCodes
     * @param index
     * @param size
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public List<ModelMainListBean> queryModelCommInfo(String storeCode, List<String> brandCodes, String storageCode,
            Integer index, Integer size) {
        List<ModelMainListBean> result = null;
        if (StringUtils.isNotBlank(storeCode) && CollectionUtils.isNotEmpty(brandCodes)) {
            Integer locIndex = null == index || index.intValue() < NUM_ZERO ? NUM_ZERO : index;
            Integer locSize = null == size || size.intValue() > MAX_SIZE ? MAX_SIZE : size;
            Map<String, Object> paramMap = new HashMap<String, Object>();
            // 分页参数
            paramMap.put("startIndex", locIndex);
            paramMap.put("maxCount", locSize);
            // 条件
            paramMap.put("storeCode", storeCode);
            paramMap.put("brandCodes", brandCodes);
            paramMap.put("storageCode", storageCode);
            result = dalClient.queryForList("MODEL_MAIN_LIST.SELECT_MODEL_COMM_INFO", paramMap,
                    ModelMainListBean.class);
        }
        return result;
    }

    /**
     * 功能描述: <br>
     * 更新样机库数量
     * 
     * @param mainList
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @SuppressWarnings("unchecked")
    public int[] updateOrDelModelCommNum(List<ModelMainListBean> mainList, boolean delFlag) {
        int[] result = null;
        if (CollectionUtils.isNotEmpty(mainList)) {
            Map<String, Object>[] batchValues = new HashMap[mainList.size()];
            for (int i = 0; i < mainList.size(); i++) {
                ModelMainListBean main = mainList.get(i);
                Map<String, Object> commonMap = new HashMap<String, Object>();
                commonMap.put("conditon_storeCode", main.getStoreCode());
                commonMap.put("conditon_commCode", main.getCommCode());
                // 更新字段-数量
                commonMap.put("commNum", main.getCommNum());
                // 新样标识
                commonMap.put("newFlag", ModelConstant.MODEL_OLD);
                batchValues[i] = commonMap;
            }
            if (delFlag) {
                // 作删除操作
                result = dalClient.batchUpdate("MODEL_MAIN_LIST.DELETE_COMM", batchValues);
            } else {
                result = dalClient.batchUpdate("MODEL_MAIN_LIST.UPDATE_COMM_NUM", batchValues);
            }
        }
        return result;
    }

    /**
     * 功能描述: <br>
     * 盘点数据-比较出异常数据
     */
    public List<ModelProblemListBean> getModelProblemList(Integer index, Integer size) {
        List<ModelProblemListBean> result = null;
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("startIndex", index);
        paramMap.put("maxCount", size);
        // 样机任务去除簇商品类型，更改为总部\大区任务类型，顾所有样机任务均为盘点任务
//        paramMap.put("taskType", ModelConstant.TASK_TYPE_CHECK);
        paramMap.put("checkTaskStatus", ModelConstant.CHECK_TASK_STATUS_DONE);
        result = dalClient.queryForList("MODEL_PROBLEM_LIST.SLECT_MODEL_PROBLEM_LIST", paramMap,
                ModelProblemListBean.class);
        return result;
    }

    /**
     * 功能描述: <br>
     * 获取需要处理盘点数据待产生异常的任务
     */
    public List<ModelProblemListBean> getModelDetailTaskList(Integer index, Integer size) {
        List<ModelProblemListBean> result = null;
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("startIndex", index);
        paramMap.put("maxCount", size);
        // 任务类型改成为总部和大区任务，且均为盘点任务
//        paramMap.put("taskType", ModelConstant.TASK_TYPE_CHECK);
        paramMap.put("checkTaskStatus", ModelConstant.CHECK_TASK_STATUS_DONE);
        result = dalClient.queryForList("MODEL_PROBLEM_LIST.SLECT_MODEL_DETAIL_TASK_CHECK", paramMap,
                ModelProblemListBean.class);
        return result;
    }

    /**
     * 功能描述: <br>
     * 批量插入更新盘点异常
     *
     * @param probList
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @SuppressWarnings("unchecked")
    public int[] upsertModelProblemList(List<ModelProblemListBean> probList) {
        int[] result = null;
        if (CollectionUtils.isNotEmpty(probList)) {
            Map<String, Object>[] batchValues = new HashMap[probList.size()];
            for (int i = 0; i < probList.size(); i++) {
                ModelProblemListBean prob = probList.get(i);
                if (null != prob) {
                    prob.setStatus(ModelConstant.PROBLEM_OPR_STATUS_ING);
                    prob.setDealStaff(prob.getCheckStaff());

                    Map<String, Object> probMap = BeanUtil.describe(prob);
                    batchValues[i] = probMap;
                }
            }
            // 更新
            result = dalClient.batchUpdate("MODEL_PROBLEM_LIST.UPSERT", batchValues);
        }
        return result;
    }

    /**
     * 功能描述: <br>
     * 更新盘点分配详情任务的盘点状态-完成比较状态2
     *
     * @param handleProbList
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @SuppressWarnings("unchecked")
    public int[] updateCheckTaskStatus(List<ModelProblemListBean> handleProbList) {
        int[] result = null;
        if (CollectionUtils.isNotEmpty(handleProbList)) {
            Map<String, Object>[] batchValues = new HashMap[handleProbList.size()];
            for (int i = 0; i < handleProbList.size(); i++) {
                ModelProblemListBean prob = handleProbList.get(i);
                if (null != prob) {
                    Map<String, Object> probMap = new HashMap<String, Object>();
                    // 更新条件
                    probMap.put("conditon_taskId", prob.getTaskId());
                    probMap.put("conditon_storeCode", prob.getStoreCode());
                    probMap.put("conditon_branch9code", prob.getBranch9code());
                    // 更新内容
                    probMap.put("checkTaskStatus", ModelConstant.CHECK_TASK_STATUS_OVER);
                    batchValues[i] = probMap;
                }
            }
            // 更新
            result = dalClient.batchUpdate("MODEL_TASK.UPDATE_CHECK_STATUS", batchValues);
        }
        return result;
    }

    /**
     * 功能描述: <br>
     * 更新超过处理时间的异常，更新状态为-待处理--->待审核
     * 
     * @param handleEndTime 超过该时间,超过几天未处理(单位天)
     * @param maxSize
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public Integer updateProbIngToAudit(Integer handleEndTime, Integer maxSize, String opr) {
        Integer result = ModelConstant.NUM_ZERO;
        Map<String, Object> paramMap = new HashMap<String, Object>();
        // 更新条件
        paramMap.put("maxSize", maxSize);
        // 指定天数，该天数之前还未处理的数据-超过该时间轮转为待审核数据
        Date handleTime = DateUtils.getNowDateShort(DateUtils.DEFAULT_TIME_PATTERN, -handleEndTime);
        paramMap.put("handleEndTime", handleTime);
        // 处理中
        paramMap.put("conditon_status", ModelConstant.PROBLEM_OPR_STATUS_ING);
        // 要更新的数据
        paramMap.put("status", ModelConstant.PROBLEM_OPR_STATUS_DONE);
        paramMap.put("dealStaff", opr);
        result = dalClient.execute("MODEL_PROBLEM_LIST.UPDATE_PROB_AUDIT_ING", paramMap);
        return result;
    }

}
